/*
 * Copyright (c) 2010 Network Security Lab (NSL), Department of Electrical
 *                    Engineering, University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: He Wu mdzz@uw.edu
 */

/**
 * @file handler.c
 * @brief Main program running on locationing and anchor nodes.
 */

#include "network.h"
#include "command.h"
#include "router.h"
#include "localization.h"
#include "socket.h"
#include "util.h"
#include "packet_handler.h"
#include "sensor.h"
#include <pthread.h>
#include <stdio.h>

#define NUM_OF_WORKERS 2  //!< number of worker threads

// debug switch
#define handler_debug 0

int main()
{
  __u16 localAddr;  // local address
  packet *in_pkt;   // received packet
  pthread_t routing_beacon_thread, timer_thread, router_thread;
  pthread_t status_report_thread;
  task_q *tq;       // task queue for handling received packets
  extern int restart_flag;
  int i;

  // welcome msg
  printf("This program receives packets from other imotes.\n");

  // set random number seed at the beginning of program
  srand(time(NULL));

  // set local Address
  if (set_addr() != 0) {
    printf("Error loading router.conf\n");
    exit(-1);
  }

  // read local address
  localAddr = get_addr();

  // initialize radio and set radio power level
  if (cc2420_init(POWER_LEVEL, localAddr) < 0) {
    printf("Radio initialization error... \n");
    exit(-1);
  }

  // initialize routing table
  initTable();

  // initialize sequence number list
  init_seq_list();

  // display thread to show the base-to-node routing table
  pthread_create(&router_thread, NULL, (void*) &displayRoutingTable, NULL);

  // thread sending out routing packets
  pthread_create(&routing_beacon_thread, NULL, (void*) &routingBeacon, NULL);

  // timer for localization algorithms
  pthread_create(&timer_thread, NULL, (void*) &local_timer, NULL);

  // thread to report sensor status back to base node
  pthread_create(&status_report_thread, NULL, (void*)&report_status_to_base, NULL);

  // create task queue
  tq = malloc(sizeof(task_q));
  task_queue_init(tq, NUM_OF_WORKERS);

  // main loop
  while (1) {
    in_pkt = malloc(sizeof(packet));

    // receive packets
    if (h_recv(localAddr, in_pkt) == 0) {
      if (handler_debug) {
        printf("\npacket port number is: %d\n", in_pkt->type);
        printf("packet is from: %x\n", in_pkt->senderAddr);
        printf("received data is: \n");
        for (i = 0; i < MAX_PAYLOAD_SIZE; i++)
          printf("%x ", in_pkt->data[i]);
        printf("\n");
      }
      // add task
      add_task(tq, in_pkt);
    }
    else
      free(in_pkt);

    // check for restart
    if (restart_flag != 0) {
      destroy_task_queue(tq);
      exit(-1);
    }
  }

  return 0;
}
